package pers.liuchengyin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.liuchengyin.constants.EsConstants;
import pers.liuchengyin.constants.MappingProperty;
import pers.liuchengyin.enums.ErrorCode;
import pers.liuchengyin.pojo.Result;
import pers.liuchengyin.service.impl.EsIndexService;
import pers.liuchengyin.utils.ResultUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName EsIndexController
 * @Description
 * @Author 柳成荫
 * @Date 2022/4/1
 */
@RestController
@RequestMapping("/es/index")
public class EsIndexController {
    @Autowired
    private EsIndexService esIndexService;

    @GetMapping("/createIndex")
    public Result<Boolean> createIndex() {
        Boolean success = esIndexService.createIndex(EsConstants.SKU_INDEX, createProperties());
        if (success) {
            return ResultUtils.successResult(Boolean.TRUE);
        }else {
            return ResultUtils.failResult(ErrorCode.SYSTEM_ERROR_NET);
        }
    }

    @GetMapping("/deleteIndex")
    public Result<Boolean> deleteIndex(String indexName) {
        Boolean success = esIndexService.deleteIndex(indexName);
        if (success) {
            return ResultUtils.successResult(Boolean.TRUE);
        }else {
            return ResultUtils.failResult(ErrorCode.SYSTEM_ERROR_NET);
        }
    }

    private Map<String, MappingProperty> createProperties(){
        Map<String, MappingProperty> properties = new HashMap<>();
        properties.put("brandId",new MappingProperty(MappingProperty.LONG));
        properties.put("brandName", new MappingProperty(MappingProperty.KEYWORD));
        properties.put("categoryId", new MappingProperty(MappingProperty.LONG));
        properties.put("categoryName", new MappingProperty(MappingProperty.KEYWORD));
        properties.put("createTime", new MappingProperty(MappingProperty.DATE));
        properties.put("hasStock", new MappingProperty(MappingProperty.BOOLEAN));
        properties.put("hotScore", new MappingProperty(MappingProperty.INTEGER));
        properties.put("name", new MappingProperty(MappingProperty.TEXT, Boolean.TRUE));
        properties.put("price", new MappingProperty(MappingProperty.LONG));
        properties.put("skuId", new MappingProperty(MappingProperty.LONG));
        properties.put("spuId", new MappingProperty(MappingProperty.LONG));
        properties.put("saleNum", new MappingProperty(MappingProperty.LONG));

        HashMap<String, MappingProperty> specProperties = new HashMap<>();
        specProperties.put("id", new MappingProperty(MappingProperty.LONG));
        specProperties.put("specName", new MappingProperty(MappingProperty.KEYWORD));
        specProperties.put("specValue", new MappingProperty(MappingProperty.KEYWORD));

        properties.put("specs", new MappingProperty(MappingProperty.NESTED, specProperties));

        return properties;
    }
}
